/*
 * DateUtils.java
 */
package behavior_analysis_control;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;


/**
 * DateUtils is a class containing static methods for validation
 * of data for the Behavior Analysis program.
 * 
 * @author Sean Brown
 * @version 11/14/2012
 *
 */
public class DateUtils {
	
	/**
	 * Date format - MM/DD/YY, HH:MM:SSAM/PM
	 */
	private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("MM/dd/yyyy, hh:mm:ssa");
	
	/**
	 * Constructor (prevents instantiation)
	 */
	private DateUtils() {
		//prevents instantiation
	}
	
	/**
	 * Validates a set of integers for date/time.
	 * 
	 * @param the_year Year
	 * @param the_month Month
	 * @param the_day Day of Month
	 * @param the_hour Hour
	 * @param the_minute Minute
	 * @param the_second Second
	 * @throws DialogErrorException Thrown when the date data is invalid.
	 */
	public static void validateDateTime(final int the_year, final int the_month, final int the_day,
			final int the_hour, final int the_minute, final int the_second) throws DialogErrorException {
		if (the_year < 1970 || the_month < 1 || the_month > 12 || the_day < 1 || the_day > 31 || the_hour < 0 ||
				the_hour > 23 || the_minute < 0 || the_minute > 59 || the_second < 0 || the_second > 59) {
			throw new DialogErrorException("The date data for this entry is invalid.");
		}
	}
	
	/**
	 * Validates a set of GPS coordinates.
	 * 
	 * @param the_latitude Latitude
	 * @param the_longitude Longitude
	 * @throws DialogErrorException Thrown when the coordinates are invalid.
	 */
	public static void validateCoords(final double the_latitude, final double the_longitude) throws DialogErrorException {
		if (the_latitude > 90.0 || the_latitude < -90.0) {
			throw new DialogErrorException("The latitude value " + the_longitude + " is out of range.");
		}
		if (the_longitude > 180.0 || the_longitude < -180.0) {
			throw new DialogErrorException("The longitude value " + the_longitude + " is out of range.");
		}
	}
	
	/**
	 * Returns a string representation of the date/time, formatted.
	 * 
	 * @param date_time long date/time
	 * @return
	 */
	public static String dateTimeFormatted(final long date_time) {
		return FORMATTER.format(date_time);
	}
	
	/**
	 * Static utility method that returns a String representation
	 * of an amount of time (long value).
	 * 
	 * @param the_time The amount of time.
	 * @return The string representation for this amount of time.
	 */
	public static String millisFormatString(final long the_time) {
		final StringBuilder sb = new StringBuilder();
		int seconds = (int) (the_time / 1000) % 60 ;
		int minutes = (int) ((the_time / (1000*60)) % 60);
		int hours   = (int) ((the_time / (1000*60*60)) % 24);
		int days = (int) ((the_time / (1000*60*60*24)));
		if (days > 0) {
			sb.append(days);
			sb.append(" Days, ");
		}
		if (hours > 0) {
			sb.append(hours);
			sb.append(" Hours, ");
		}
		if (minutes > 0) {
			sb.append(minutes);
			sb.append(" Minutes, ");
		}
		sb.append(seconds);
		sb.append(" Seconds");
		return sb.toString();
	}
	
	public static long dateToMillis(final int the_year, final int the_month, final int the_day, final int the_hour, final int the_minute, 
			final int the_second) {
		final GregorianCalendar date = new GregorianCalendar(Locale.getDefault());
		date.set(the_year, the_month - 1, the_day, the_hour, the_minute, the_second);
		return date.getTimeInMillis();
	}
}
